package com.jgy.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jgy.annotation.SetLogValue;
import com.jgy.entity.Permission;
import com.jgy.entity.SpaceUser;
import com.jgy.mapper.PermissionMapper;
import com.jgy.mapper.SpaceUserMapper;
import com.jgy.utils.CommonConstants;
import com.jgy.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 姜光远
 * @date 2022/10/20 15:34
 */
@RestController
@RequestMapping("/perssion")
//@CrossOrigin
public class PermissionController {

    @Resource
    PermissionMapper mapper;
    @Resource
    private SpaceUserMapper userMapper;


    @GetMapping("/getlist")
    public R getList(){
        String username = "admin";
        LambdaQueryWrapper<SpaceUser> query = new LambdaQueryWrapper<>();
        query.eq(StringUtils.isNotBlank(username),SpaceUser::getUsername,username);
        SpaceUser mdosUser = userMapper.selectOne(query);
//        List<Permission> permissionList = mapper.selectList(null);
//        List<UserRole> userRoles = roleMapper.getUserRoleByUserId(1);
//        System.out.println(userRoles+"roles");
//        System.out.println(rolePermissionMapper.selectList(null)+"rolePermissionMapper");
//        System.out.println(userRoleMapper.selectList(null)+"userRoleMapper");
//        return R.ok(permissionList);
        return R.ok(mdosUser);
    }
    @SetLogValue(modular = "权限模块", type = CommonConstants.SELECT, describe = "查询用户权限")
    @GetMapping("/getPermissList")
    public R<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Permission> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Permission::getName, search);
        }
        Page<Permission> permissionPage = mapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return R.ok(permissionPage);
    }
    @SetLogValue(modular = "权限模块", type = CommonConstants.ADD, describe = "新增权限")
    //保存
    @PostMapping("/savePermiss")
    public R<?> savePermiss(@RequestBody Permission permission) {
        int i = mapper.insert(permission);
        if (i>0){
            return R.ok("保存成功！");
        }
        return R.failed("保存失败!");
    }
    @SetLogValue(modular = "权限模块", type = CommonConstants.DELETE, describe = "删除权限")
    //删除
    @DeleteMapping("/deleteById")
    public R<?> deleteById(@RequestParam(value = "id") Long id) {
        int i = mapper.deleteById(id);
        if (i>0){
            return R.ok("删除成功！");
        }
        return R.failed("删除失败!");
    }
    @SetLogValue(modular = "权限模块", type = CommonConstants.UPDATE, describe = "更新权限")
    //修改
    @PutMapping("/update")
    public R<?> update(@RequestBody Permission permission) {
        mapper.updateById(permission);
        return R.ok();
    }


    @GetMapping("/all")
    public R<List<Permission>> all() {
        return R.ok(mapper.selectList(null));
    }
}
